package lelouet.datacenter.simulation.vms.webserver;

/** a strategy to select sequences of long */
public interface LongIterator {

	public long nextLength(long lastTime);


	/** an {@link LengthIterator} that always return the same length. */
	public static class Constant implements LongIterator {
		final long length;

		public Constant(long length) {
			this.length = length;
		}

		public long getLength() {
			return length;
		}

		public long nextLength(long lastTime) {
			return length;
		}
	}

	public static class RoundRobin implements LongIterator {

		protected int nextPos = -1;

		protected long[] longs = new long[] { 0 };

		/**
		 * constructs a new RoundRobin.
		 * 
		 * @param delays
		 *            the list of long to return. If null or empty,
		 *            {@link #nextLength(long)} will always return 0
		 */
		public RoundRobin(long... delays) {
			if (delays != null && delays.length > 0) {
				longs = delays;
			}
		}

		protected void next() {
			nextPos++;
			if (nextPos >= longs.length) {
				nextPos = -1;
			}
		}

		@Override
		public long nextLength(long lastTime) {
			next();
			return longs[nextPos];
		}

	}
}